﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization;
using System.ComponentModel;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework;
using System.Threading;

namespace Push.Entities
{
    [DataContractAttribute]
    public class SoundManager
    {
        public enum Sounds
        {
            Hit = 0,
        };

        private static bool mute;
        [DataMemberAttribute]
        public static bool Mute
        {
            get
            {
                return mute;
            }
            set
            {
                mute = value;
                if (mute)
                {
                    //foreach (SoundEffectInstance sound in HitSound)
                    //{
                    //    if (sound.State == SoundState.Playing)
                    //    {
                    //        sound.Stop();
                    //    }
                    //}
                }
            }
        }

        static SoundEffectInstance[,] HitSound;
        
        static Random rnd;

        static int channel = 0;

        public static void PlayDiscHit(double Volume)
        {
            if (HitSound != null && !SoundManager.Mute)
            {
                FrameworkDispatcher.Update();

                int index;

                if (Volume < 0.25)
                    index = 4;
                else if (Volume < 0.45)
                    index = 3;
                else if (Volume < 0.7)
                    index = 2;
                else if (Volume < 0.9)
                    index = 1;
                else index = 0;

                HitSound[channel, index].Pitch = (float)(-1.0 + (Volume + rnd.NextDouble()) * 0.5);
                HitSound[channel, index].Play();

                channel = (channel + 1) % 4;
            }
        }

        public SoundManager()
        {
            //MoveSound = moveSound.CreateInstance();
        }

        static SoundManager()
        {
            rnd = new Random();

            //SoundEffect[] hitSound = new SoundEffect[5];
            //HitSound = new SoundEffectInstance[4, 5];

            //hitSound[0] = SoundEffect.FromStream(Application.GetResourceStream(new Uri("/Curling;component/Sounds/Hit/Hit100.wav", UriKind.Relative)).Stream);//TitleContainer.OpenStream("Sounds_/Hit/Hit100.wav"));
            //hitSound[1] = SoundEffect.FromStream(Application.GetResourceStream(new Uri("/Curling;component/Sounds/Hit/Hit80.wav", UriKind.Relative)).Stream);//TitleContainer.OpenStream("Sounds_/Hit/Hit100.wav"));
            //hitSound[2] = SoundEffect.FromStream(Application.GetResourceStream(new Uri("/Curling;component/Sounds/Hit/Hit60.wav", UriKind.Relative)).Stream);//TitleContainer.OpenStream("Sounds_/Hit/Hit100.wav"));
            //hitSound[3] = SoundEffect.FromStream(Application.GetResourceStream(new Uri("/Curling;component/Sounds/Hit/Hit40.wav", UriKind.Relative)).Stream);//TitleContainer.OpenStream("Sounds_/Hit/Hit100.wav"));
            //hitSound[4] = SoundEffect.FromStream(Application.GetResourceStream(new Uri("/Curling;component/Sounds/Hit/Hit20.wav", UriKind.Relative)).Stream);//TitleContainer.OpenStream("Sounds_/Hit/Hit100.wav"));

            //for (int i = 0; i < 4; i++)
            //{                
            //    HitSound[i, 0] = hitSound[0].CreateInstance();                
            //    HitSound[i, 1] = hitSound[1].CreateInstance();                
            //    HitSound[i, 2] = hitSound[2].CreateInstance();                
            //    HitSound[i, 3] = hitSound[3].CreateInstance();                
            //    HitSound[i, 4] = hitSound[4].CreateInstance();
            //}
        }
    }
}
